package com.woldier.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woldier.common.R;
import com.woldier.entity.Employee;
import com.woldier.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("login")
    @ResponseBody
    /*注解为RequestBody获取json数据 */
    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {
        /*将密码加密*/
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        /*更具提交的username查找数据*/
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);

        /*判断是否有该数据*/
        /*若没有登陆失败*/
        if (one == null) {
            return R.error("登陆失败");
        }

        /*如果密码不匹配*/
        if (!one.getPassword().equals(password))
            return R.error("密码不正确");

        /*判断账户状态*/
        if (one.getStatus() == 0)
            return R.error("用户禁用");

        /*允许登录设置sqssion*/
        session.setAttribute("employee", one.getId());

        return R.success(one);

    }

    /**
     * 推出登录
     *
     * @return
     */
    @PostMapping("logout")
    @ResponseBody
    public R<Employee> loginOut(HttpSession session) {

        /*删除session*/
        session.removeAttribute("employee");

        return R.success(null);

    }


    /**
     * 员工的添加
     */

    @PostMapping
    public R addEmployee(@RequestBody Employee employee, HttpSession session) {
        log.info("新增员工{}", employee.toString());
        /*将密码加密*/
        /*设置初始密码未123456*/
        String password = "123456";
        employee.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        /*现在通过MyMetaObjectHandler来设置对应数据*/
//        /*设置时间*/
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        /*设置更新人*/
//        Long id = (Long) session.getAttribute("employee");
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);

        /*插入*/
        employeeService.save(employee);


        return R.success("新增成功");
    }


    /**
     * 分页查询数据
     * http://localhost/employee/page?page=1&pageSize=10
     *
     * @return 返回的时page 里面封装了数据以及页信息
     */

    @GetMapping("page")

    public R<Page> getPage(int page, int pageSize, String name) {
        /*构造分页器*/
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        /*构造条件构造器*/
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        /*添加过滤条件*/
        /*此一个类型为boolean类型 传入True队徽吧过滤条件加入*/
        queryWrapper.like(name != null ? true : false, Employee::getName, name);
        /*添加排序条件*/
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        /*调用查询*/
        employeeService.page(pageInfo, queryWrapper);
        /*返回结果*/
        return R.success(pageInfo);
    }

    /**
     * http://localhost/employee
     * 请求方法: PUT
     * 根据id修改用户信息
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R updateEmployeeStatus(@RequestBody Employee employee) {


        /*设置更新时间和更新该操作的用户*/
        /*现在通过MyMetaObjectHandler来设置对应数据*/
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) session.getAttribute("employee"));

        /*调用service*/
        employeeService.updateById(employee);

        return R.success("更新成功");
    }


    /**
     * 根据员工id获取信息
     */
    @GetMapping("{id}")
    public R getById(@PathVariable(name = "id") Long id) {

        Employee employee = employeeService.getById(id);

        return R.success(employee);
    }


}
